package com.wanlian.router.ui.me.message;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import com.smart.android.ui.app.IntentExtra;
import com.smart.android.ui.tools.BaseListFragment;
import com.smart.android.ui.web.WebActivity;
import com.wanlian.router.R;
import com.wanlian.router.bean.Message;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hyu on 2018/8/31.
 * Email: fvaryu@qq.com
 */
public class SystemNoticeFragment extends BaseListFragment {

    private SystemNoticeAdapter mSystemNoticeAdapter;
    private List<Message> mSystemNotices;

    public static SystemNoticeFragment newInstance() {
        return new SystemNoticeFragment();
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);

        mSystemNotices = new ArrayList<>();
        setAdapter(mSystemNoticeAdapter = new SystemNoticeAdapter(getActivity(), mSystemNotices));

        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Message message = mSystemNotices.get(position);
                if (!message.isHasLook()) {
                    message.setHasLook(true);
                    mSystemNoticeAdapter.notifyDataSetChanged();
                    NetWorkingClient.getInstance().updateMessageRead(getActivity(), message.getId(), new INetCallBack() {
                        @Override
                        public void onFinish(ResponseData response, @Nullable Object o) {

                        }
                    });
                }

                if (TextUtils.isEmpty(message.getUrl())) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(IntentExtra.EXTRA_OBJ, message);

                    redirectActivity(MessageDetailActivity.class, bundle);
                } else {
                    WebActivity.fire(getActivity(), message.getUrl());
                }
            }
        });
    }

    @Override
    protected void initData(View view) {
        super.initData(view);


        getEmptyLayout().setImageTipEmptyResId(R.drawable.image_empty_message);
        setEmptyText("还没有公告哦~");
    }


    @Override
    protected void loadData(final boolean isRefresh) {
        super.loadData(isRefresh);
        if (!canToLoad(isRefresh)) {
            return;
        }

        NetWorkingClient.getInstance().querySysMessage(getActivity(), getPageInfo(), new INetCallBack<List<Message>>() {
            @Override
            public void onFinish(ResponseData response, @Nullable List<Message> systemNotices) {
                resetPull();
                if (response.isSuccess()) {
                    if (isRefresh) {
                        mSystemNotices.clear();
                    }
                    if (systemNotices != null) {
                        mSystemNotices.addAll(systemNotices);
                    }

                    mSystemNoticeAdapter.notifyDataSetChanged();
                }

                if (mSystemNotices.isEmpty()) {
                    showEmpty();
                }
            }
        });
    }
}
